package com.itheima.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.common.SmsUtil;
import com.itheima.common.ValidateCodeUtils;
import com.itheima.pojo.R;
import com.itheima.pojo.User;
import com.itheima.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@Slf4j
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    private RedisTemplate redisTemplate;
    @PostMapping("/sendMsg")
    public R<String> sendMsg(@RequestBody User user, HttpSession session){
        //获取手机号
        String phone = user.getPhone();
        if(StringUtils.isNotEmpty(phone)){
            //生成随机的4位验证码
            String code = ValidateCodeUtils.generateValidateCode(4).toString();
            log.info("code={}",code);

//            //调用阿里云提供的短信服务API完成发送短信
//            SmsUtil.sendSms(phone,code);
//
//            //需要将生成的验证码保存到Session
//            session.setAttribute(phone,code);

            redisTemplate.opsForValue().set(phone,code,5, TimeUnit.MINUTES);
            return R.success("手机验证码短信发送成功");
        }
        return R.error("短信发送失败");
    }


    @PostMapping("/login")
    public R login(@RequestBody Map map, HttpSession session) {
        log.info("手机验证码登录");
        String phone = map.get("phone").toString();
        String code = map.get("code").toString();
//        Object codeInSession = session.getAttribute(phone);

        Object codeInSession = redisTemplate.opsForValue().get(phone);

        if (codeInSession != null && codeInSession.equals(code)) {
            LambdaQueryWrapper<User> lqw = new LambdaQueryWrapper<User>();
            lqw.eq(User::getPhone, phone);
            User loginuser = userService.getOne(lqw);
            if (loginuser == null) {
                loginuser = new User();
                loginuser.setPhone(phone);
                loginuser.setStatus(1);
                userService.save(loginuser);
            }
        Long userId = loginuser.getId();
        session.setAttribute("user",userId);
        redisTemplate.delete(phone);
        return R.success(loginuser);
        }
        return R.error("登录失败!");

    }

//    @PostMapping("login")
//    public R login(String phone, String code){
//        log.info("用户登录");
//
//        HashMap<String, Object> map = new HashMap<>();
//        map.put("code",0);
//        if(StringUtils.isEmpty(phone)||StringUtils.isEmpty(code)){
//            map.put("msg","用户名或密码为空!");
//        }
//
//        LambdaQueryWrapper<User> lqw = new LambdaQueryWrapper<>();
//        lqw.eq(User::getPhone, phone);
//        User loginUser = userService.getOne(lqw);
//        if(loginUser==null){
//
//        }
//
//
//
//    }





    @PostMapping("/loginout")
    public R loginOut(HttpServletRequest request){
        request.getSession().removeAttribute("user");
        return R.success("退出成功!");
    }


}

